import {
  DictId,
  FormItemKey,
  ObjectType,
  Pagination,
  ResponseDataPromise,
} from 'typings/interfaces'

export interface Option {
  itemText: string
  itemValue: string | boolean | number
  disabled?: boolean
  children?: any
}

export enum Types {
  redio = 'redio',
  input = 'input',
  select = 'select',
  inputNumber = 'input-number',
  user = 'user',
  datePick = 'date-pick',
  upload = 'upload',
  selectOrg = 'selectOrg',
  selectBridge = 'selectBridge',
  patrolTime = 'patrolTime',
  radio = 'radio',
  image = 'image',
  cascader = 'cascader',
  autocomplete = 'autocomplete',
}

export type Alias = string

interface BaseFormItem {
  key: FormItemKey
  type: Types
  required: boolean
  labelId: string
  placeholderId?: string
  width?: number
  class?: string
  error?: string
  disabled?: boolean
  widthSpan?: number
  page?: string
  option?: any
  editable?: boolean
}

export interface Input extends BaseFormItem {
  placeholderId?: string
  row?: number
  inputType?: 'text' | 'textarea'
  /** 
   *  超过多少字节数展示文字提示
   */
  tooltipLength?: number
}

export interface Autocomplete extends BaseFormItem {
  shouldUpdate?: Array<[x: FormItemKey, x?: Alias]>
}
export interface Select extends BaseFormItem {
  multiple?: boolean
  collapseTags?: boolean
  alias?: Alias
  shouldUpdate?: Array<[x: FormItemKey, x?: Alias]>
  linkage?: {
    key: FormItemKey
    dictMap: Record<string, string[]>
  }
  remoteMethod: (
    pagination: Omit<Pagination, 'total'>,
    params: ObjectType
  ) => ResponseDataPromise<ObjectType> // 分页查询接口
  linkageSync?: {
    key: FormItemKey
    dictMap: (params: ObjectType) => ResponseDataPromise<ObjectType>
    label?: string
    value?: string
  }
}

export interface InputNumber extends BaseFormItem {
  max?: number
  min?: number
  precision: number /** 精度 */
  step?: number /** 步长*/
}

export interface DatePick extends BaseFormItem {
  formate: string
  dateType:
  | 'year'
  | 'month'
  | 'date'
  | 'week'
  | 'datetime'
  | 'daterange'
  | 'monthrange'
  | 'datetimerange'
  disabledDate: (Date) => boolean
  defaultValue: string
}

// 用户
export interface User extends BaseFormItem {
  multiple: boolean
  idListName: string
}

// 上传文件
export interface Upload extends BaseFormItem {
  multiple: boolean
  limit: number
}

//机构树形选择
export interface SelectOrg extends BaseFormItem {
  filterName: string
}

export type FormItem =
  | Select
  | Input
  | DatePick
  | InputNumber
  | User
  | Upload
  | SelectOrg
  | Cascader
  | Autocomplete

export type FormItems = FormItem[]

export type Options = Record<DictId, Option[] | Array<any>>

export interface SelectBridge extends BaseFormItem {
  multiple?: boolean
  collapseTags?: boolean
  alias?: Alias
  page?: Alias
  linkage?: {
    key: FormItemKey
    dictMap: Record<string, string[]>
  }
  remoteMethod: (
    pagination: Omit<Pagination, 'total'>,
    params: ObjectType
  ) => ResponseDataPromise<ObjectType> // 分页查询接口
}

export interface Cascader extends BaseFormItem {
  alias?: Alias
  shouldUpdate?: Array<[x: FormItemKey, x?: Alias]>
  props?: ObjectType
}
